package com.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Admin;
import com.pojo.MovieList;
import com.service.DyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@RestController("DyAction")
@RequestMapping("/dy")
public class DyAction {

    @Autowired
    private DyService dyService;

    @Value("${dy.home}")
    private String home;

    @RequestMapping("/search")
    public ResponseBean  search(HttpServletRequest request,String name, @RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "50") int size)
    {
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        if (admin==null){
            return new ResponseBean(403,"没有权限",null);
        }

        IPage result = dyService.searchName(name,page,size);
        return new ResponseBean(0,result.getTotal(),result.getRecords());
    }

    @RequestMapping("/down")
    public ResponseBean  down(Long id, HttpServletRequest request)
    {
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        if (admin==null){
            return new ResponseBean(403,"没有权限",null);
        }
       MovieList m = dyService.getById(id);
       if (m==null)
       {
           return new ResponseBean(404,"电影不存在",null);
       }
       String name = m.getMd5();
        File file = new File(home+"dy",name);
        if (file.exists()==false)
        {
            return new ResponseBean(404,"电影文件不存在",null);
        }
        file.delete();
        m.setIsdown(0);
        dyService.updateById(m);
        return new ResponseBean(200,"操作成功",null);

    }

}
